package com.example.wxordersystem.web;

import com.example.wxordersystem.entity.ProductCategory;
import com.example.wxordersystem.entity.ProductInfo;
import com.example.wxordersystem.exception.SellException;
import com.example.wxordersystem.form.ProductCategoryForm;
import com.example.wxordersystem.form.ProductForm;
import com.example.wxordersystem.service.CategoryService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author 常红伟
 */
@RestController
@RequestMapping("/seller/category")
@Slf4j
public class SellCategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page" , defaultValue = "1") Integer page,
                             @RequestParam(value = "size" , defaultValue = "10")Integer size,
                             Map<String,Object> map){

        List<ProductCategory> productCategoryList= categoryService.findAll();
        System.out.println(categoryService.findAll());
        System.out.println(productCategoryList.size()+"------------------------------------------");
        map.put("productCategoryList",productCategoryList);
        map.put("page",page);
        map.put("size",size);
        return new ModelAndView("category/list",map);
    }

    @PostMapping("/save")
    public ModelAndView save(@Valid ProductCategoryForm productCategoryForm,
                             BindingResult bindingResult,
                             Map<String,Object>map){

        if (bindingResult.hasErrors()){
            map.put("msg",bindingResult.getFieldError().getDefaultMessage());
            map.put("url","index");
            return new ModelAndView("common/error",map);
        }

        ProductCategory productCategory = new ProductCategory();
        try {
            if(productCategoryForm.getCategoryId() == null || productCategoryForm.getCategoryId()==0){
                BeanUtils.copyProperties(productCategoryForm,productCategory);
                categoryService.save(productCategory);
            }else {
                productCategory=categoryService.findOne( productCategoryForm.getCategoryId());
                BeanUtils.copyProperties(productCategoryForm,productCategory);
                categoryService.update(productCategory);
            }
        }catch (SellException e){
            map.put("msg",e.getMessage());
            map.put("url","index");
            return new ModelAndView("common/error",map);
        }

        map.put("url","list");
        return new ModelAndView("common/success",map);

    }


    /**
     * 列表
     * @param
     * @param map
     * @return
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId", required = false) Integer categoryId,
                              Map<String, Object> map) {
        if (!StringUtils.isEmpty(categoryId)) {
            ProductCategory productCategory = categoryService.findOne(categoryId);
            map.put("productCategory", productCategory);
        }

        //查询所有的类目
        List<ProductCategory> categoryList = categoryService.findAll();
        map.put("categoryList", categoryList);

        return new ModelAndView("category/index", map);
    }

}
